package cn.jn.lhm.teacher.service.impl;

import cn.jn.lhm.service.config.exception.GlobalException;
import cn.jn.lhm.teacher.entity.EduChapter;
import cn.jn.lhm.teacher.entity.EduVideo;
import cn.jn.lhm.teacher.entity.chapter.ChapterVo;
import cn.jn.lhm.teacher.entity.chapter.VideoVo;
import cn.jn.lhm.teacher.mapper.EduChapterMapper;
import cn.jn.lhm.teacher.mapper.EduVideoMapper;
import cn.jn.lhm.teacher.service.EduChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/2 - 21:34
 */
@Service
public class EduChapterServiceImpl implements EduChapterService {

    @Resource
    private EduChapterMapper eduChapterMapper;

    @Resource
    private EduVideoMapper eduVideoMapper;


    @Override
    public List<ChapterVo> getAll(String courseId) {
        //存放组装后的ChapterVo
        ArrayList<ChapterVo> list = new ArrayList<>();

        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = eduChapterMapper.selectList(chapterQueryWrapper);
        //遍历数组，组装对象
        chapterList.forEach(chapter -> {
            QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
            videoQueryWrapper.eq("chapter_id", chapter.getId());
            List<EduVideo> videoList = eduVideoMapper.selectList(videoQueryWrapper);
            //组装对象
            ChapterVo chapterVo =  pageChapterVo(chapter, videoList);
            list.add(chapterVo);
        });
        return list;
    }

    @Override
    public void save(EduChapter eduChapter) {
        eduChapterMapper.insert(eduChapter);
    }

    @Override
    public EduChapter getChapterById(String id) {
        return eduChapterMapper.selectById(id);
    }

    @Override
    public void updateById(EduChapter eduChapter) {
        eduChapterMapper.updateById(eduChapter);
    }


    @Override
    public boolean deleteById(String id) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        Integer count = eduVideoMapper.selectCount(wrapper);
        if (count > 0){
            throw new GlobalException(20001, "不能删除该章节");
        }else {
            int result = eduChapterMapper.deleteById(id);
            return result > 0 ;
        }
    }

    /**
     * 树形结构组装章节的数据
     * @param chapter 每一章节的信息
     * @param videoList 每一章节对应的所有的小节的信息
     * @return 组装后的章节信息
     */
    private ChapterVo pageChapterVo(EduChapter chapter, List<EduVideo> videoList) {
        //用来存放小节信息
        ArrayList<VideoVo> list = new ArrayList<>();
        ChapterVo chapterVo = new ChapterVo();
        BeanUtils.copyProperties(chapter, chapterVo);
        videoList.forEach(eduVideo -> {
            VideoVo videoVo = new VideoVo();
            BeanUtils.copyProperties(eduVideo, videoVo);
            list.add(videoVo);
        });
        chapterVo.setChildren(list);
        return chapterVo;
    }
}
